import heapq
from typing import List


class Solution:
    def connectSticks(self, sticks: List[int]) -> int:
        ans = 0
        heapq.heapify(sticks)
        while len(sticks) >= 2:
            n1 = heapq.heappop(sticks)
            n2 = heapq.heappop(sticks)
            ans += n1 + n2
            heapq.heappush(sticks, n1 + n2)
        return ans


if __name__ == "__main__":
    print(Solution().connectSticks(sticks=[2, 4, 3]))  # 14
    print(Solution().connectSticks(sticks=[1, 8, 3, 5]))  # 30
    print(Solution().connectSticks(sticks=[5]))  # 0
